#!/bin/bash

# This is a simple build sh-script for the client
# and all given plugins.
# I will port it to ANT some day...

PARAM=$1

# Plugin dirs that should be build
PLUGIN_DIRS="PlugInONE"

############# Compile all, build jars and execute ###############
buildAll() {
    echo "+ ============ clean up... "
    rm `find . -name "*.class"` > /dev/null
    echo "+ ============ set CLASSPATH... "
    echo "+ ============ compile..."
    javac -deprecation -d ./bin ./src/*.java \
    && export CLASSPATH=$CLASSPATH:.:`pwd`/bin \
    && echo "+ ============ build jar... " \
    && ./jar_me.sh \
    && echo "+ ============ build plugins... " \
    && cd plugin_builds \
    && for PLUGIN in $PLUGIN_DIRS; do cd $PLUGIN ; ./make.sh || exit ; cd .. ; done \
    && cd .. \
    && echo "+ ============ copy plugin jars to bin... " \
    && cp -v `find ./plugin_builds -name "*.jar"` ./bin/jar/plugins \
    && echo "+ ============ start jar..." \
    && java -jar ./bin/jar/jtalkerclient.jar \
    && echo "+ ============ make backup " \
    && mydir=`pwd` \
    && cd .. && tar cfvz $mydir.auto-backup.tar.gz $mydir > /dev/null \
    && echo "+ ============ All OK. "
    echo "ready." 
}

startTwoClients() {
    echo "+ ============ copy plugin jars to bin... " \
    && cp -v `find ./plugin_builds -name "*.jar"` ./bin/jar/plugins \
    && echo "+ ============== start clients "
    java -jar ./bin/jar/jtalkerclient.jar &
    java -jar ./bin/jar/jtalkerclient.jar &
    
    echo "+ ============ All OK. "
    echo "ready." 
}

copyPluginJars() {
    echo "+ ============ copy plugin jars to bin... "
    rm ./bin/jar/plugins/*.jar
    cp -v `find ./plugin_builds -name "*.jar"` ./bin/jar/plugins
}

if [ "$PARAM" = "exec" ] ; then
    startTwoClients
    exit
fi
if [ "$PARAM" = "plugin" ] ; then
    copyPluginJars
    exit
fi

buildAll

